home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Text / Edit / GoldED-Demo / installdata / golded / developer / source / project / main.c next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  5.5 KB  |  256 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.   Read the project list (example).
  4.  
  5.   DICE:
  6.  
  7.   dcc main.c -// -3.0 -o project
  8.  
  9.  
  10.   This C source code demonstrates reading the project list using ARexx (so that
  11.   you can develop external project management tools compatible with GoldED).
  12.  
  13.   ------------------------------------------------------------------------------
  14. */
  15.  
  16. /// "includes & prototypes"
  17.  
  18. #include <exec/exec.h>
  19. #include <rexx/errors.h>
  20. #include <rexx/rxslib.h>
  21. #include <clib/exec_protos.h>
  22. #include <clib/rexxsyslib_protos.h>
  23.  
  24. #define Prototype extern
  25.  
  26. ///
  27. /// "prototypes"
  28.  
  29. Prototype void            FreeRexxCommand(struct RexxMsg *);
  30. Prototype void            ListTree       (struct List *);
  31. Prototype void            main           (ULONG, char **);
  32. Prototype struct RexxMsg *SendRexxCommand(UBYTE *, UBYTE *, struct MsgPort *);
  33. Prototype ULONG           WaitForAnswer  (struct MsgPort *, UBYTE *);
  34.  
  35. ///
  36. /// "defines"
  37.  
  38. // editor's internal configuration format is based on "objects"
  39.  
  40. struct Object {
  41.  
  42.     struct Node Node;                                // it's a linked list
  43.  
  44.     UWORD            ID;                             // object ID
  45.     UWORD            Type;                           // class ID
  46.     struct Class    *Class;                          // class base
  47.  
  48.     union {                                          // object's data section
  49.  
  50.         ULONG        Number;                         // number
  51.         UBYTE       *String;                         // string
  52.         struct List *List;                           // object list
  53.         APTR         Data;                           // memory block
  54.  
  55.     } Value;
  56. };
  57.  
  58. // object types (bit 0-2)
  59.  
  60. #define OBJECT_TYPE_NUMBER 1
  61. #define OBJECT_TYPE_STRING 2
  62. #define OBJECT_TYPE_LIST   3
  63. #define OBJECT_TYPE_DATA   4
  64.  
  65. #define OBJECTTYPE(a) (((struct Object *)(a))->Type & 0x7)
  66.  
  67. ///
  68. /// "globals"
  69.  
  70. UBYTE Version[] = "$VER: PRJ 1.3 (" __COMMODORE_DATE__ ")";
  71.  
  72. ///
  73. /// "main"
  74.  
  75. void
  76. main(argc, argv)
  77.  
  78. ULONG argc;
  79. char *argv[];
  80. {
  81.     struct MsgPort *replyPort;
  82.  
  83.     UBYTE *host = "GOLDED.1";
  84.  
  85.     if (replyPort = CreateMsgPort()) {
  86.  
  87.         if (SendRexxCommand(host, "LOCK CURRENT RELEASE=4", replyPort)) {
  88.  
  89.             UBYTE result[80];
  90.  
  91.             if (WaitForAnswer(replyPort, result) == RC_OK) {
  92.  
  93.                 if (SendRexxCommand(host, "QUERY PRJLIST", replyPort)) {
  94.  
  95.                     if (WaitForAnswer(replyPort, result) == RC_OK) {
  96.  
  97.                         struct List   *list;
  98.  
  99.                         if (list = (struct List *)atol(result)) {
  100.  
  101.                             if (list->lh_Head->ln_Succ) {
  102.  
  103.                                 ListTree(list);
  104.                             }
  105.                             else
  106.                                 puts("project list is empty");
  107.                         }
  108.                     }
  109.                 }
  110.  
  111.                 if (SendRexxCommand(host, "UNLOCK", replyPort))
  112.  
  113.                     WaitForAnswer(replyPort, result);
  114.             }
  115.         }
  116.  
  117.         DeleteMsgPort(replyPort);
  118.     }
  119.  
  120.     exit(0);
  121. }
  122.  
  123.  
  124. /* --------------------------------- ListTree ----------------------------------
  125.  
  126.  Print project configuration (this is a recursive function)
  127.  
  128. */
  129.  
  130. void
  131. ListTree(list)
  132.  
  133. struct List *list;
  134. {
  135.     struct Object *object;
  136.  
  137.     for (object = (struct Object *)list->lh_Head; object->Node.ln_Succ; object = (struct Object *)object->Node.ln_Succ) {
  138.  
  139.         UBYTE *label = (object->Node.ln_Name) ? object->Node.ln_Name : "UNNAMED";
  140.  
  141.         if (OBJECTTYPE(object) == OBJECT_TYPE_LIST) {
  142.  
  143.             printf("category [%s]\n", label);
  144.  
  145.             ListTree(object->Value.List);
  146.         }
  147.         else
  148.             puts(label);
  149.     }
  150. }
  151.  
  152. ///
  153. /// "ARexx"
  154.  
  155. /* -------------------------------------- WaitForAnswer -----------------------
  156.  
  157.   Wait for answer on previously sent message. Free message afterwards. Primary
  158.   return code is returned, the result string (if any) written to <result>.
  159.  
  160. */
  161.  
  162. ULONG
  163. WaitForAnswer(port, result)
  164.  
  165. struct MsgPort *port;
  166. UBYTE          *result;
  167. {
  168.     struct RexxMsg *rexxMsg;
  169.     ULONG  error;
  170.  
  171.     *result = NULL;
  172.  
  173.     do {
  174.         
  175.         WaitPort(port);
  176.  
  177.         if (rexxMsg = (struct RexxMsg *)GetMsg(port)) {
  178.  
  179.             if ((error = rexxMsg->rm_Result1) == RC_OK) {
  180.  
  181.                 if (rexxMsg->rm_Result2)
  182.  
  183.                     strcpy(result, (UBYTE *)rexxMsg->rm_Result2);
  184.             }
  185.         }
  186.  
  187.     } while (!rexxMsg);
  188.  
  189.     FreeRexxCommand(rexxMsg);
  190.  
  191.     return(error);
  192. }
  193.  
  194.  
  195. /* ------------------------------------- FreeRexxCommand ----------------------
  196.  
  197.  Free ARexx message
  198.  
  199. */
  200.  
  201. void
  202. FreeRexxCommand(rexxmessage)
  203.  
  204. struct RexxMsg *rexxmessage;
  205. {
  206.     if (rexxmessage->rm_Result1 == RC_OK) 
  207.  
  208.         if (rexxmessage->rm_Result2)
  209.  
  210.             DeleteArgstring((UBYTE *)rexxmessage->rm_Result2);
  211.  
  212.     DeleteArgstring((UBYTE *)ARG0(rexxmessage));
  213.  
  214.     DeleteRexxMsg(rexxmessage);
  215. }
  216.  
  217.  
  218. /* ---------------------------------- SendRexxCommand -------------------------
  219.  
  220.  Send ARexx message
  221.  
  222. */
  223.  
  224. struct RexxMsg *
  225. SendRexxCommand(port, cmd, replyPort)
  226.  
  227. struct MsgPort *replyPort;
  228. UBYTE          *cmd, *port;
  229. {
  230.     struct MsgPort *rexxport;
  231.     struct RexxMsg *rexx_command_message;
  232.  
  233.     rexx_command_message = NULL;
  234.  
  235.     Forbid();
  236.  
  237.     if (rexxport = FindPort(port)) {
  238.  
  239.         if (rexx_command_message = CreateRexxMsg(replyPort, NULL, NULL)) {
  240.  
  241.             if (rexx_command_message->rm_Args[0] = CreateArgstring(cmd, strlen(cmd))) {
  242.  
  243.                 rexx_command_message->rm_Action = RXCOMM | RXFF_RESULT;
  244.  
  245.                 PutMsg(rexxport, &rexx_command_message->rm_Node);
  246.             }
  247.         }
  248.     }
  249.  
  250.     Permit();
  251.  
  252.     return(rexx_command_message);
  253. }
  254.  
  255. ///
  256.